import { serverLoading } from '@library/utils';

const linkType = ['dataLink', 'dataLinkCombox', 'dataLinkComBox'];

export const getServer = async (url: string, data: any) => {
  return await serverLoading(url, data);
};

export function getShowComponentsArray(options: {
  columns: any[], dataIndex: number, keyValue: string, list: any[];
}) {
  let _dataObj: any = {};
  const { columns, list, dataIndex, keyValue } = options;

  let _list = columns?.filter((item: any) => linkType.includes(item.type)
    && item.dataIndex !== dataIndex
    && item.options?.bindName !== keyValue
  );

  let _outList = list?.filter((item: any) => linkType.includes(item.type));

  _list?.forEach((element: any) => {
    _dataObj[element.key!] = element?.options?.fieldNames || [];
  });

  _outList?.forEach((element: any) => {
    _dataObj[element.options.formItemProps.name] = element?.options?.inputProps.fieldNames || [];
  });

  let _filterList = _outList?.map((item: any) => ({ label: item?.options.formItemProps.label, value: item?.options.formItemProps.name, id: item?.options.formItemProps.name }));

  return {
    list: _list?.map((item: any) => ({ label: item?.title, value: item?.key, id: item?.key })).concat(_filterList),
    dataObj: _dataObj
  };
}